package com.rules.admin.controller;

import com.gillion.grule.client.components.InParameterComponent;
import com.gillion.grule.client.core.variable.PackageVariable;

import com.gillion.grule.client.sync.DroolsRulePackage;

import com.rules.admin.entity.ParserContext;
import com.rules.admin.entity.RulePackage;
import com.rules.admin.parser.DroolsRuleParser;
import com.rules.admin.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping({"/rules/esb"})
@Controller
public class RuleGetController {
   Logger logger = LoggerFactory.getLogger(InParameterComponent.class);
   @Autowired
   DroolsRuleParser droolsRuleParser;
   @Autowired
   private RulePackageService rulePackageService;
   @Autowired
   private RuleVariableService ruleVariableService;
   @Autowired
   private DataSourceModelService dataSourceModelService;
   @Autowired
   private RemoteService remoteService;
   @Autowired
   private DubboService dubboService;

   @Autowired
   private ValidatorService validatorService;
   @Autowired
   private SpringCloudService springCloudService;
   @Autowired
   private GroovyShellService groovyShellService;

   @RequestMapping(
      value = {"/packages"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public DroolsRulePackage getRule(@RequestBody RulePackage rulePackage) {
      RulePackage rp;
      if (rulePackage.getGroupId().equals("-")) {
         rp = this.rulePackageService.getRulePackageByArtifactId(rulePackage.getArtifactId(), rulePackage.getVersion());
      } else {
         rp = this.rulePackageService.getRulePackage(rulePackage.getGroupId(), rulePackage.getArtifactId(), rulePackage.getVersion());
      }

      DroolsRulePackage droolsRulePackage = this.droolsRuleParser.parser(rp, new ParserContext(false));
      droolsRulePackage.setStatus(rp.getPackageStatus());
      PackageVariable packageVariable = this.ruleVariableService.loadCompletelyPackageVaribles(rp.getId());
      droolsRulePackage.setVariables(packageVariable);
      droolsRulePackage.setDataSources(this.dataSourceModelService.selectMapByProjectCode(rp.getGroupId()));
      droolsRulePackage.setRemoteConfig(this.remoteService.selectListByProjectCode(rp.getGroupId()));
      droolsRulePackage.setDubboConfigs(this.dubboService.selectListByProjectCode(rp.getGroupId()));
      droolsRulePackage.setSpringCloudConfig(this.springCloudService.selectListByProjectCode(rp.getGroupId()));
      droolsRulePackage.setGroovyShellConfigs(this.groovyShellService.selectListByProjectCode(rp.getGroupId()));
      droolsRulePackage.setValidatorConfig(this.validatorService.findByProjectId(rp.getProjectid()));
      this.logger.info("获取规则编码：" + rulePackage.getArtifactId() + ",内容：" + droolsRulePackage.getContent());
      return droolsRulePackage;
   }
}
